package com.seaboxdata.threadHandler.electronic;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.ElectronicCoupletInfo;
import com.seaboxdata.kafka.BaseKafkaConsumer;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.service.ElectronicCoupletService;
import com.seaboxdata.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * 水库KAFKA接入
 *
 * 接收前置机数据  ->
 *
 * @author
 * @create 2021-05-25 11:37
 **/
@Component
@Slf4j
public class ElectronicCoupletHandler implements DisposableBean, Runnable {

    @Value("${electronic.ElectronicCoupletController.Topic}")
    private String topic;

    private Thread thread;

    @Autowired
    private BaseKafkaConsumer baseKafkaConsumer;
    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;
    private ElectronicCoupletService electronicCoupletService;
    private boolean flag;

    @Value("${interface.isTest}")
    private String isTest;

    @Value("${electronic.isTest}")
    private String isTestElec;

    @Autowired
    public void initThread() {
        if ("true".equals(isTestElec)) {
            return;
        }
        this.flag = true;
        this.thread = new Thread(this);
        thread.setName("ElectronicCoupletHandlerThread");
        this.thread.start();
        log.info("ElectronicCoupletHandler start");
    }

    @Override
    public void run() {
        log.info("ElectronicCoupletHandler 接收数据中");
        try {
            KafkaConsumer<String, String> consumer = baseKafkaConsumer.getConsumer(topic);
            while (flag) {
                boolean success = true;
                ConsumerRecords<String, String> messages = consumer.poll(Duration.ofMillis(100L));
                List<ElectronicCoupletInfo> results = new ArrayList<>();
                for (ConsumerRecord<String, String> message : messages) {
                    ElectronicCoupletInfo electronicCoupletInfo = JSONObject
                            .parseObject(message.value(), ElectronicCoupletInfo.class);
                    electronicCoupletInfo.setCreateTime(DateUtil.now());
                    results.add(electronicCoupletInfo);
                }
                //写入内置机KAFKA
                if (!CollectionUtils.isEmpty(results)) {
                    try {
                        log.info("ElectronicCoupletHandler 写入内置机KAFKA {}条",results.size());
                        baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(results), topic);
                        consumer.commitSync();
                    } catch (Exception e) {
                        log.error("ElectronicCoupletHandler commit offset failed");
                        e.printStackTrace();
                        //success = false;
                    }
                }
            }
        } catch (Exception e) {
            log.error("ElectronicCoupletHandler run error");
            e.printStackTrace();
            //success = false;
        }
    }

    @Override
    public void destroy() throws Exception {
        log.info("ElectronicCoupletHandlerThread is down");
        this.flag = false;
    }

}
